{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c331a65d-8144-4611-b49d-db33a98bfa76",
   "metadata": {},
   "outputs": [],
   "source": [
    "class FinancialRecord:  \n",
    "    def __init__(self):  \n",
    "        self.income_records = []  \n",
    "        self.expense_records = []  \n",
    "  \n",
    "    def add_income(self, date, amount, category, note):  \n",
    "        if amount <= 0:  \n",
    "            print(\"收入金额必须为正数\")  \n",
    "            return  \n",
    "        self.income_records.append({  \n",
    "            \"date\": date,  \n",
    "            \"amount\": amount,  \n",
    "            \"category\": category,  \n",
    "            \"note\": note  \n",
    "        })  \n",
    "        print(\"收入记录添加成功\")  \n",
    "  \n",
    "    def add_expense(self, date, amount, category, note):  \n",
    "        if amount <= 0:  \n",
    "            print(\"支出金额必须为正数\")  \n",
    "            return  \n",
    "        self.expense_records.append({  \n",
    "            \"date\": date,  \n",
    "            \"amount\": amount,  \n",
    "            \"category\": category,  \n",
    "            \"note\": note  \n",
    "        })  \n",
    "        print(\"支出记录添加成功\")  \n",
    "  \n",
    "    def print_income_records(self):  \n",
    "        print(\"\\n收入记录:\")  \n",
    "        for record in self.income_records:  \n",
    "            print(f\"日期: {record['date']}, 金额: {record['amount']}, 类别: {record['category']}, 备注: {record['note']}\")  \n",
    "  \n",
    "    def print_expense_records(self):  \n",
    "        print(\"\\n支出记录:\")  \n",
    "        for record in self.expense_records:  \n",
    "            print(f\"日期: {record['date']}, 金额: {record['amount']}, 类别: {record['category']}, 备注: {record['note']}\")  \n",
    "  \n",
    "  \n",
    "# 示例使用  \n",
    "if __name__ == \"__main__\":  \n",
    "    financial_record = FinancialRecord()  \n",
    "  \n",
    "    # 添加收入  \n",
    "    financial_record.add_income(\"2023-10-01\", 5000, \"工资\", \"本月工资\")  \n",
    "    financial_record.add_income(\"2023-10-05\", 2000, \"奖金\", \"项目奖金\")  \n",
    "  \n",
    "    # 添加支出  \n",
    "    financial_record.add_expense(\"2023-10-02\", 150, \"餐饮\", \"早餐\")  \n",
    "    financial_record.add_expense(\"2023-10-03\", 300, \"交通\", \"打车费\")  \n",
    "    financial_record.add_expense(\"2023-10-04\", 1000, \"购物\", \"购买衣物\", -500)  # 尝试添加负数金额，应提示错误  \n",
    "  \n",
    "    # 打印记录  \n",
    "    financial_record.print_income_records()  \n",
    "    financial_record.print_expense_records()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "67ab1cb5-1ab4-4c4b-95e8-4f2a0e52cf74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收入记录添加成功\n",
      "收入记录添加成功\n",
      "支出记录添加成功\n",
      "支出记录添加成功\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "FinancialRecord.add_expense() takes 5 positional arguments but 6 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 52\u001b[0m\n\u001b[0;32m     50\u001b[0m financial_record\u001b[38;5;241m.\u001b[39madd_expense(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m2023-10-02\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m150\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m餐饮\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m早餐\u001b[39m\u001b[38;5;124m\"\u001b[39m)  \n\u001b[0;32m     51\u001b[0m financial_record\u001b[38;5;241m.\u001b[39madd_expense(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m2023-10-03\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m300\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m交通\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m打车费\u001b[39m\u001b[38;5;124m\"\u001b[39m)  \n\u001b[1;32m---> 52\u001b[0m \u001b[43mfinancial_record\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_expense\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2023-10-04\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1000\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m购物\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m购买衣物\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m500\u001b[39;49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# 尝试添加负数金额，应提示错误  \u001b[39;00m\n\u001b[0;32m     54\u001b[0m \u001b[38;5;66;03m# 打印记录  \u001b[39;00m\n\u001b[0;32m     55\u001b[0m financial_record\u001b[38;5;241m.\u001b[39mprint_income_records()  \n",
      "\u001b[1;31mTypeError\u001b[0m: FinancialRecord.add_expense() takes 5 positional arguments but 6 were given"
     ]
    }
   ],
   "source": [
    "class FinancialRecord:  \n",
    "    def __init__(self):  \n",
    "        self.income_records = []  \n",
    "        self.expense_records = []  \n",
    "  \n",
    "    def add_income(self, date, amount, category, note):  \n",
    "        if amount <= 0:  \n",
    "            print(\"收入金额必须为正数\")  \n",
    "            return  \n",
    "        self.income_records.append({  \n",
    "            \"date\": date,  \n",
    "            \"amount\": amount,  \n",
    "            \"category\": category,  \n",
    "            \"note\": note  \n",
    "        })  \n",
    "        print(\"收入记录添加成功\")  \n",
    "  \n",
    "    def add_expense(self, date, amount, category, note):  \n",
    "        if amount <= 0:  \n",
    "            print(\"支出金额必须为正数\")  \n",
    "            return  \n",
    "        self.expense_records.append({  \n",
    "            \"date\": date,  \n",
    "            \"amount\": amount,  \n",
    "            \"category\": category,  \n",
    "            \"note\": note  \n",
    "        })  \n",
    "        print(\"支出记录添加成功\")  \n",
    "  \n",
    "    def print_income_records(self):  \n",
    "        print(\"\\n收入记录:\")  \n",
    "        for record in self.income_records:  \n",
    "            print(f\"日期: {record['date']}, 金额: {record['amount']}, 类别: {record['category']}, 备注: {record['note']}\")  \n",
    "  \n",
    "    def print_expense_records(self):  \n",
    "        print(\"\\n支出记录:\")  \n",
    "        for record in self.expense_records:  \n",
    "            print(f\"日期: {record['date']}, 金额: {record['amount']}, 类别: {record['category']}, 备注: {record['note']}\")  \n",
    "  \n",
    "  \n",
    "# 示例使用  \n",
    "if __name__ == \"__main__\":  \n",
    "    financial_record = FinancialRecord()  \n",
    "  \n",
    "    # 添加收入  \n",
    "    financial_record.add_income(\"2023-10-01\", 5000, \"工资\", \"本月工资\")  \n",
    "    financial_record.add_income(\"2023-10-05\", 2000, \"奖金\", \"项目奖金\")  \n",
    "  \n",
    "    # 添加支出  \n",
    "    financial_record.add_expense(\"2023-10-02\", 150, \"餐饮\", \"早餐\")  \n",
    "    financial_record.add_expense(\"2023-10-03\", 300, \"交通\", \"打车费\")  \n",
    "    financial_record.add_expense(\"2023-10-04\", 1000, \"购物\", \"购买衣物\", -500)  # 尝试添加负数金额，应提示错误  \n",
    "  \n",
    "    # 打印记录  \n",
    "    financial_record.print_income_records()  \n",
    "    financial_record.print_expense_records()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cde17a6-475c-4bc5-90ef-673ae734a5de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
